package com.lost.octopus.es.helper.entity.wrapper;

import com.lost.octopus.es.helper.entity.model.FieldConfig;
import com.lost.octopus.es.helper.constant.EsMatchType;

import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import lombok.extern.log4j.Log4j2;

/**
 * 单值的前缀查询
 * 同 sql like xx%
 * <br/>
 * 注解：{@code @EsSearch(queryType = EsQueryPrefixWrapper.class)}
 * <br/>
 * 示例：{@code "3502"}
 * <br/>
 * 效果：{@code {"prefix":{"xzqh":{"value":"350212"}}} }
 * @author zhangbh
 **/
@Log4j2
@Component
public class EsQueryPrefixWrapper extends AbstractQueryWrapper {

    @Resource
    private EsQueryWrapper esQueryWrapper;

    @Override
    public QueryBuilder addWrapper(BoolQueryBuilder builder, FieldConfig field) {
        FieldConfig copy = field.copy().setMatchType(EsMatchType.prefix);
        return esQueryWrapper.addWrapper(builder, copy);
    }
}
